package com.jdd.common.util;

import com.jdd.modules.sdkidentifyresultpushlog.entity.enums.PropertiesFileNameKind;
import com.jdd.modules.sdkidentifyresultpushlog.entity.enums.PropertiesTypeKind;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @version 1.0
 * @Description:
 * @author： jdd尚岳
 * @Date: 2022-04-07 11:17
 */
@Slf4j
public class PropertyUtil {
    /** 项目环境 */
    private static final String profilesName = "";
    /** 配置文件中车场CODEkey */
    private static final String parkCode = "";

    /**
     * 去读properties文件的内容，保存到Map中
     *
     * @param propsName
     * @return
     */
    public static String readProperties() {
        try {
            String profiles = getValue(PropertiesFileNameKind.APPLICATION.value(), profilesName);
            if(StringUtils.isNotBlank(profiles)){
                // 默认开发环境
                String profilesFileName = PropertiesFileNameKind.APPLICATION_DEV.value();
                if(PropertiesTypeKind.TEST.value().equals(profiles)){
                    // 测试环境
                    profilesFileName = PropertiesFileNameKind.APPLICATION_TEST.value();
                }else if(PropertiesTypeKind.PROD.value().equals(profiles)){
                    // 生产环境
                    profilesFileName = PropertiesFileNameKind.APPLICATION_PROD.value();
                }
                return getValue(profilesFileName, parkCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String getValue(String fileName,String attrName){
        Properties props = new Properties();
        try {
            InputStream in = PropertyUtil.class.getClassLoader().getResourceAsStream(fileName);
            props.load(in);
            return props.getProperty(attrName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
